package util;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import constant.Constant;

import java.net.InetAddress;


/**
 * Created by chenkuo on 2018/3/13.
 */
public class JschManager {

    private static volatile Session session;

    public static class SshEntity{
        private String username;
        private String password;
        private String prvKey;
        private String hostname;
        private String passphrase;

        public SshEntity(String username, String password, String prvKey, String hostname, String passphrase) {
            this.username = username;
            this.password = password;
            this.prvKey = prvKey;
            this.hostname = hostname;
            this.passphrase = passphrase;
        }
    }

    private static volatile boolean isInit = false;
//    private final static String[] proxyList = new String[]{Constant.sshProt,Constant.redisProt,Constant.reportProt};//ck可以映射多个远程端口至本地
    private final static String[] proxyList = new String[]{"3308_10.30.59.58_6371"};//ck可以映射多个远程端口至本地
//    private final static String[] proxyList = new String[]{"3309_10.31.141.54_3306"};//ck可以映射多个远程端口至本地

    public static void main(String[] args) {
        JschManager.init();
    }
    public static void init(){
        if(isInit)
            return ;
        //获取动态配置
         String hostname = "sshtest.bingex.com"; //ssh主机， 123.57.51.92
        String username = "kuo.chen"; // ssh 账号
        String password = "password"; // ssh 账号密码；已经使用密钥方式了就不需要密码了
        String prvkey = "C:\\ssh\\id_rsa_bingyao"; // 私钥路径

        SshEntity sshEntity = new SshEntity(username, password, prvkey, hostname, null);
        Session session = createSession(sshEntity);



        //设置本地代理
        try {
            for(String l : proxyList){
                String[] proxy = l.split("_");
                int lPort = Integer.valueOf(proxy[0]);
                String rhost = proxy[1];
                int rport = Integer.valueOf(proxy[2]);
                 session.setPortForwardingL(InetAddress.getLocalHost().getHostAddress(), lPort, rhost, rport);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        isInit = true;
    }

    protected static Session createSession(SshEntity s){

        JSch jsch = new JSch();
        try {

            session = jsch.getSession(s.username, s.hostname, 22);
            session.setConfig("StrictHostKeyChecking","no"); //关闭gssapi认证,存在性能问题，故设置成
            jsch.addIdentity(s.prvKey);
            session.connect(3000);   // making a connection with timeout.
            if(session.isConnected()){
                System.out.println("session connect success");
            }else{
                System.out.println("session connect fail");
            }

        } catch (JSchException e) {
            e.printStackTrace();
        }
        return  session;
    }

    public static void desotrySession(){
        session.disconnect();
    }

}
